package logic

import (
	"context"
	"doctor-medicine-system/doctorrpc/doctorrpc"
	"doctor-medicine-system/doctorrpc/internal/svc"
	"doctor-medicine-system/models/mysql"
	"doctor-medicine-system/pkg"
	"errors"

	"github.com/zeromicro/go-zero/core/logx"
)

type DoctorLoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewDoctorLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DoctorLoginLogic {
	return &DoctorLoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 登录及注册
func (l *DoctorLoginLogic) DoctorLogin(in *doctorrpc.DoctorLoginRequest) (*doctorrpc.DoctorLoginResponse, error) {
	// todo: add your logic here and delete this line

	doctor := mysql.Doctor{
		Mobile:   in.Mobile,
		Password: pkg.Md5(in.Password),
	}

	if err := doctor.DoctorLoginAdd(mysql.DB, in.Mobile); err != nil {
		return nil, err
	}

	if doctor.Password != pkg.Md5(in.Password) {
		return nil, errors.New("密码错误")
	}

	return &doctorrpc.DoctorLoginResponse{
		DoctorId: int64(doctor.ID),
	}, nil
}
